//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.mass.core.activiti.workflow.service;

import com.mass.core.utils.WorkflowUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

@Service
public class WorkflowProcessDefinitionService {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    protected RuntimeService runtimeService;
    @Autowired
    protected RepositoryService repositoryService;
    @Autowired
    protected HistoryService historyService;

    public WorkflowProcessDefinitionService() {
    }

    public ProcessDefinition findProcessDefinitionByPid(String processInstanceId) {
        HistoricProcessInstance historicProcessInstance = (HistoricProcessInstance)this.historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        String processDefinitionId = historicProcessInstance.getProcessDefinitionId();
        ProcessDefinition processDefinition = this.findProcessDefinition(processDefinitionId);
        return processDefinition;
    }

    public ProcessDefinition findProcessDefinition(String processDefinitionId) {
        ProcessDefinition processDefinition = (ProcessDefinition)this.repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
        return processDefinition;
    }

    public void deployFromClasspath(String exportDir, String... processKey) throws Exception {
        DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
        String[] processKeys = new String[]{"leave", "leave-dynamic-from", "leave-formkey", "dispatch"};
        String[] var8 = processKeys;
        int var7 = processKeys.length;

        for(int var6 = 0; var6 < var7; ++var6) {
            String loopProcessKey = var8[var6];
            if(ArrayUtils.isNotEmpty(processKey)) {
                if(ArrayUtils.contains(processKey, loopProcessKey)) {
                    this.deploySingleProcess(resourceLoader, loopProcessKey, exportDir);
                } else {
                    this.logger.debug("module: {} not equals process key: {}, ignore and continue find next.", loopProcessKey, processKey);
                }
            } else {
                this.deploySingleProcess(resourceLoader, loopProcessKey, exportDir);
            }
        }

    }

    private void deploySingleProcess(ResourceLoader resourceLoader, String processKey, String exportDir) throws IOException {
        String classpathResourceUrl = "classpath:/deployments/" + processKey + ".bar";
        this.logger.debug("read workflow from: {}", classpathResourceUrl);
        Resource resource = resourceLoader.getResource(classpathResourceUrl);
        InputStream inputStream = resource.getInputStream();
        if(inputStream == null) {
            this.logger.warn("ignore deploy workflow module: {}", classpathResourceUrl);
        } else {
            this.logger.debug("finded workflow module: {}, deploy it!", classpathResourceUrl);
            ZipInputStream zis = new ZipInputStream(inputStream);
            Deployment deployment = this.repositoryService.createDeployment().addZipInputStream(zis).deploy();
            List<?> list = this.repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).list();
            Iterator<?> var11 = list.iterator();

            while(var11.hasNext()) {
                ProcessDefinition processDefinition = (ProcessDefinition)var11.next();
                WorkflowUtils.exportDiagramToFile(this.repositoryService, processDefinition, exportDir);
            }
        }

    }

    public void redeploy(String exportDir, String... processKey) throws Exception {
        this.deployFromClasspath(exportDir, processKey);
    }

    public void deployAllFromClasspath(String exportDir) throws Exception {
        this.deployFromClasspath(exportDir, new String[0]);
    }
}
